//
//  BCLineChartController.swift
//  Blockfolio
//
//  Created by Maybe on 11/05/2021.
//

import UIKit
import Charts

class BCLineChartController: BCBaseController {

    var lineChartView: LineChartView = LineChartView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "BTC/USDT"
        
        addLineChart()
        
        chartDescription()
        
        interactionStyle()
        
        setBackgroundBorder()
        
        setXAxisStyle()
        
        setYAxisStyle()
        
        upData()
    }
    
//    fileprivate lazy var lineChartView:LineChartView = {
//
//        let lineChartView = LineChartView.init(frame: CGRect.init(x: 0, y: 20, width: Screen_width, height: 400))
//
//        return lineChartView
//    }()
}

extension BCLineChartController {
    func addLineChart() {
        lineChartView.backgroundColor = UIColor.black
        lineChartView.delegate = self
        lineChartView.frame = CGRect.init(x: 0, y: 20, width: Screen_width, height: 400)
        self.view .addSubview(lineChartView)
    }
     //交互样式
    func interactionStyle() {
        lineChartView.scaleYEnabled = false   //y轴缩放
        lineChartView.doubleTapToZoomEnabled = true  //双击缩放
        lineChartView.dragEnabled = true  //是否可以拖动
        lineChartView.dragDecelerationEnabled = true //拖动惯性
        lineChartView.dragDecelerationFrictionCoef = 0.9 //阻尼系数
    }
    
    func chartDescription(){
        lineChartView.noDataText = "暂无数据"
        lineChartView.chartDescription?.text = "折线图描述"
        lineChartView.chartDescription?.font = UIFont.systemFont(ofSize: 12)
        lineChartView.chartDescription?.textColor = Color_line
        lineChartView.legend.textColor = Color_line
        lineChartView.legend.formSize = 10
        lineChartView.legend.form = Legend.Form.none
    }
    
    func setBackgroundBorder() {
        lineChartView.drawBordersEnabled = true
        lineChartView.borderColor = UIColor.hex("#100E17")
        lineChartView.borderLineWidth = 1.0
    }
    
    //设置x轴样式
    func setXAxisStyle() {
        lineChartView.xAxis.axisLineWidth = 0.5   //x轴宽度
        lineChartView.xAxis.axisLineColor = Color_line  //x轴颜色
        lineChartView.xAxis.axisMinimum = 0   //最小刻度
        lineChartView.xAxis.axisMaximum = 10  //最大刻度
        
        lineChartView.xAxis.labelPosition = .bottom //x轴显示在下方
        lineChartView.xAxis.labelTextColor = UIColor.white
        lineChartView.xAxis.labelFont = .systemFont(ofSize: 12)
        
        //文字格式
        let formatter = NumberFormatter() //根据选择的时间，如果选择的是每天，则显示5.10  5.11 等等
        lineChartView.xAxis.valueFormatter = DefaultAxisValueFormatter(formatter: formatter)
        
        //网格
        lineChartView.xAxis.drawGridLinesEnabled = true  //x轴网格线
        lineChartView.xAxis.gridColor = Color_line   //颜色
        lineChartView.xAxis.gridLineWidth = 0.5      //宽度
//        lineChartView.xAxis.gridLineDashLengths = [4,2] // 虚线
        
    }
    
    func setYAxisStyle() {
        lineChartView.rightAxis.enabled = false
        lineChartView.rightAxis.drawAxisLineEnabled = false
        lineChartView.rightAxis.enabled = false
        
        lineChartView.leftAxis.gridColor = Color_line
        lineChartView.leftAxis.gridLineWidth = 0.5
        lineChartView.leftAxis.drawZeroLineEnabled = true

//        lineChartView.leftAxis.zeroLineColor = .white
//        lineChartView.leftAxis.zeroLineWidth = 1
        lineChartView.leftAxis.zeroLineDashLengths = [4,2]
        
    }
}

extension BCLineChartController {
    func upData(){
        var dataEntries = [ChartDataEntry]()
        for i in 0..<20 {
            let y = 50 - arc4random()%50
            let entry = ChartDataEntry.init(x: Double(i), y: Double(y))
            dataEntries.append(entry)
        }
        let chartDataSet = LineChartDataSet(entries:dataEntries,label: "测试")
        chartDataSet.setColor(UIColor.hex("#1AB927"))
        chartDataSet.lineWidth = 3
//        chartDataSet.lineDashLengths = [4,2] //虚线
        chartDataSet.mode = .horizontalBezier
        chartDataSet.drawCirclesEnabled = false
        chartDataSet.drawCircleHoleEnabled = false
        let chartData = LineChartData(dataSet: chartDataSet)
        lineChartView.data = chartData
        lineChartView.animate(xAxisDuration: 1)
    }
}



//MARK:- ChartViewDelegate

extension BCLineChartController:ChartViewDelegate {
    func selectChartView(_ chartView: ChartViewBase,entry: ChartDataEntry, highlight: Highlight){
        BClog("选中了\(entry)")
    }
    
    func noSelectChartView(_ charView: ChartViewBase){
        BClog("取消选择")
        
        //重新渲染
        charView.data?.notifyDataChanged()
        charView.notifyDataSetChanged()
    }
    
    func scaleChartView(_ chartView:ChartViewBase, scaleX: CGFloat, scaleY: CGFloat) {
        BClog("捏合了\(scaleX)")
    }
    
    func translateChartView(_ chartView: ChartViewBase, tX:CGFloat, tY: CGFloat)  {
        BClog("拖拽了\(tX)")
    }
    
}
